package cate.game.mail.po;

import cate.common.game.GameCollection;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import cp.solution.util.collection.ListKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Document(GameCollection.NAME_MAILBOX)
@NoteClass(value = "邮箱", detail = "_id同master._id")
public class MailBox {

    public static final String AUTO_DELETE_FIELD = "updateDate";

    @NoteField(value = "邮箱版本号", detail = "开发调试用，非业务需要")
    public String version = "1.0";

    @NoteField(value = "邮箱数据更新日期")
    public Date updateDate;

    @NoteField(value = "未领取的邮件列表")
    private List<Mail> list;

    @NoteField(value = "角色唯一id")
    @Id
    public String uid;

    public void initialize() {
        if (list == null) {
            list = new ArrayList<>();
        }
        // 邮箱最多存60封
        list = ListKit.trimMaxKeepTail(list, 60);
        if (updateDate == null) {
            updateDate = new Date();
        }
    }

    public synchronized void add(Mail item) {
        this.list.add(item);
        this.updateDate = new Date();
    }

    public synchronized void addToRole(Role role) {
        for (Mail item : this.list) {
            role.getMail().add(item);
        }
        this.list.clear();
    }

    public boolean isEmpty(){
        return CollectionUtils.isEmpty(list);
    }
}
